/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */


function TreeNode(val, left, right) {
  this.val = (val === undefined ? 0 : val)
  this.left = (left === undefined ? null : left)
  this.right = (right === undefined ? null : right)
}

var t = new TreeNode(1, new TreeNode(2, new TreeNode(3, null, null), new TreeNode(4, null, null)), new TreeNode(2, new TreeNode(4, null, null), new TreeNode(3, null, null)))



/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {


  if (root.left && root.right) {
    let front = [root.left, root.right]
    let level = 1
    while (front.length > 0) {
      let tmpCount = Math.pow(2, level)
      let tmpArr = []
      while (tmpCount > 0) {
        let tmpNode = front.shift()
        tmpArr.push(tmpNode.val)
        if (tmpNode.left) {
          front.push(tmpNode.left)
        } else {
          front.push(new TreeNode(null, null, null))
        }
        if (tmpNode.right) {
          front.push(tmpNode.right)
        } else {
          front.push(new TreeNode(null, null, null))
        }
        tmpCount--
      }
      let l = tmpArr.length
      if (l % 2 == 0) {
        for (let i = 0; i < l / 2; i++) {
          if (tmpArr[i] != tmpArr[l - 1 - i]) return false
        }
      } else {
        return false
      }
    }
    return true
  }
  return root.left ^ root.right
};


isSymmetric = function (root) {
  const check = function (left, right) {
    if (!(left ^ right)) {
      if (left == null) return true
      return left.val == right.val && check(left.left, right.right) && check(left.right, right.left)
    } else {
      return false
    }
  }
  return check(root, root)
}
console.log(isSymmetric(t));